package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.EquipmentInfo;
import com.hushao.lims.weblims.domain.EquipmentRepertory;
import com.hushao.lims.weblims.domain.EquipmentScrap;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.repository.EquipmentScrappedRepository;
import com.hushao.lims.weblims.service.EquipmentRepertoryService;
import com.hushao.lims.weblims.service.EquipmentScrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 930588706
 * @date 18/1/22
 */
@Service
public class EquipmentScrapServiceImpl implements EquipmentScrapService {
    @Autowired
    private EquipmentRepertoryService equipmentRepertoryService;
    @Autowired
    private EquipmentScrappedRepository equipmentScrapRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentScrap applyScrapped(EquipmentInfo equipmentInfo, Integer equipmentCount, TeacherInfo teacherInfo) throws Exception {
        EquipmentRepertory equipmentRepertory=equipmentRepertoryService.findByEquipment(equipmentInfo);
        if(equipmentRepertory.getEquipmentCount()<equipmentCount){
            throw new Exception("当前报废器材数量不得大于其库存数量！");
        }
        EquipmentScrap equipmentScrapped=new EquipmentScrap(equipmentInfo,equipmentCount,teacherInfo,null,null);
        return equipmentScrapRepository.save(equipmentScrapped);
    }

    @Override
    public Page<EquipmentScrap> listEquipmentScrappedByTeacherAndEquipmentName(String equipmentName, TeacherInfo teacher, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        return equipmentScrapRepository.findByEquipmentInfo_EquipmentNameLikeAndTeacherInfo(equipmentName,teacher,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(Long equipmentScrappedId) throws Exception {
        EquipmentScrap equipmentScrapped=equipmentScrapRepository.getOne(equipmentScrappedId);
        if(equipmentScrapped==null){
            throw  new Exception("此器材报废记录不存在！");
        }else if(!"0".equals(equipmentScrapped.getEquipmentScrapState())){
            throw new Exception("此器材报废记录已被审核！");
        }
        equipmentScrapRepository.delete(equipmentScrapped);
    }

    @Override
    public Page<EquipmentScrap> listEquipmentScrappedByEquipmentNameLikeAndStateLike(String equipmentName, String state, Pageable pageable) {
        equipmentName="%"+equipmentName+"%";
        state="%"+state+"%";
        return equipmentScrapRepository.findByEquipmentInfo_EquipmentNameLikeAndEquipmentScrapStateLike(equipmentName,state,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public EquipmentScrap audit(Long equipmentScrapId, String state, TeacherInfo teacher) throws Exception {
        EquipmentScrap equipmentScrap=equipmentScrapRepository.getOne(equipmentScrapId);
        if(equipmentScrap==null){
            throw new Exception("此器材报废记录不存在！");
        }
        if(!"0".equals(equipmentScrap.getEquipmentScrapState())){
            throw new Exception("此器材报废记录已被审核！");
        }
        //其器材库存去除报废数量
        Integer count=equipmentScrap.getEquipmentCount();
        if("1".equals(state)){
            EquipmentInfo equipmentInfo=equipmentScrap.getEquipmentInfo();
            EquipmentRepertory equipmentRepertory=equipmentRepertoryService.findByEquipment(equipmentInfo);
            if(equipmentRepertory==null||equipmentRepertory.getEquipmentCount()<count){
                throw new Exception("此器材库存没有或小于报废数量,无法报废！");
            }
            equipmentRepertory.setEquipmentCount(equipmentRepertory.getEquipmentCount()-count);
            equipmentRepertoryService.saveOrUpdate(equipmentRepertory);
        }
        equipmentScrap.setEquipmentScrapAudit(teacher);
        equipmentScrap.setEquipmentScrapState(state);
        return equipmentScrapRepository.save(equipmentScrap);
    }
}
